package com.yk.anima.servlet;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yk.anima.dao.AdminMapper;
import com.yk.anima.dao.OrdersMapper;
import com.yk.anima.dao.ShoppingCartMapper;
import com.yk.anima.pojo.Admin;
import com.yk.anima.pojo.Orders;
import com.yk.anima.pojo.ProductsCat;
import com.yk.anima.pojo.ShoppingCart;
import com.yk.anima.response.CommonCode;
import com.yk.anima.response.QueryResponseResult;
import com.yk.anima.response.QueryResult;
import com.yk.anima.response.ResponseResult;
import com.yk.anima.util.DateUtils;
import com.yk.anima.util.SnowflakeIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author xiaokai
 * @create 2021-05-14 10:26:14
 * @desc...订单业务
 */

@Service
public class OrdersServlet {

    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    ShoppingCartMapper shoppingCartMapper;


    public List<Orders> findAll(int orderAdminId) {
        List<Orders> all = ordersMapper.findAll(orderAdminId);
        if (all.size()==0){
            return null;
        }
        return all;
    }

    public ResponseResult save(int orderAdminId, int id) {

        Admin admin = adminMapper.selectByPrimaryKey(orderAdminId);
        if (admin == null) {
            return new ResponseResult(CommonCode.FAIL);
        }
        ShoppingCart shoppingCart = shoppingCartMapper.selectByPrimaryKey(id);
        if (shoppingCart == null) {
            return new ResponseResult(CommonCode.FAIL);
        }
        try {
            Orders orders = new Orders();
            orders.setAddress(admin.getAddress());
            orders.setUserName(admin.getUserName());
            orders.setGender(admin.getGender());
            orders.setPhone(admin.getPhone());
            orders.setData(DateUtils.parseDate2String(new Date(), "yyyy-mm-dd hh:mm:ss"));
            long l = new SnowflakeIdUtils(3, 1).nextId();
            String s = String.valueOf(l);
            orders.setOrderNumber(s);
            System.out.println("当前订单编号 " + new SnowflakeIdUtils(3, 1));
            orders.setPrice(shoppingCart.getPrice());
            orders.setAnimalName(shoppingCart.getName());
            orders.setOrderAdminId(admin.getaId());
            System.out.println(orders);
            ordersMapper.insertSelective(orders);
            System.out.println(" 当前下单时间 " + DateUtils.parseDate2String(new Date(), "yyyy-mm-dd hh:mm:ss"));

        } catch (Exception e) {
            return new ResponseResult(CommonCode.FAIL);
        }


        return new ResponseResult(CommonCode.SUCCESS);
    }


    public QueryResponseResult likeName( int page, int size,String animalName) {
        if (page <= 0) {
            page = 0;
        }
        if (size <= 0) {
            size = 2;
        }
        if (animalName == null || "".toString().equals(animalName)) {
            animalName = "";
        }
        PageHelper.startPage(page, size);
        Page<Orders> catPage = ordersMapper.findByName(animalName);
        List<Orders> list = catPage.getResult();
        long total = catPage.getTotal();

        QueryResult<Orders> queryResult = new QueryResult<Orders>();

        queryResult.setList(list);
        queryResult.setTotal(total);
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    public ResponseResult delete() {
        ordersMapper.delete();
        return new ResponseResult(CommonCode.SUCCESS);
    }

    public ResponseResult deleteById(int oId,int orderAdminId) {
        int number = ordersMapper.deleteByPrimaryKey(oId, orderAdminId);
        if (number>0){
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);

    }
}
